package com.foreknow.service;

import com.foreknow.dao.impl.ReplyDaoImpl;
import com.foreknow.entity.Reply;

import java.sql.SQLException;
import java.util.List;

public class ReplyService {
	private ReplyDaoImpl replyDaoImpl;

	public ReplyService() {
		replyDaoImpl = new ReplyDaoImpl();
	}

	/**
	 * 根据Blog的ID获取相应的回复ID
	 * 
	 * @param blogId
	 * @return
	 */
	public List<Reply> getRepliesByBlogId(Integer blogId) {
		String sql = "select * from reply where blogId = ?";
		try {
			return replyDaoImpl.queryByCondition(sql, blogId);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}

	public Reply getReplyById(Integer id) {
		String sql = "select * from reply where id = ?";
		try {
			return replyDaoImpl.queryOne(sql, id);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 新建一条回复
	 * 
	 * @param reply
	 * @return
	 */
	public int insertReply(Reply reply) {
		String sql = "insert into reply(blogId,replyId,authorId,answererId,content,uploadTime) values(?,?,?,?,?,?)";
		Object[] params = { reply.getBlogId(), reply.getReplyId(), reply.getAuthorId(), reply.getAnswererId(),
				reply.getContent(), reply.getUploadTime() };
		try {
			return replyDaoImpl.insert(sql, params);
		} catch (SQLException e) {
			e.printStackTrace();
			return -1;
		}
	}

	/**
	 * 根据回复ID删除该条回复
	 * 
	 * @param id
	 * @return
	 */
	public int deleteReplyById(Integer id) {
		String sql = "delete from reply where id = ?";
		try {
			return replyDaoImpl.delete(sql, id);
		} catch (SQLException e) {
			e.printStackTrace();
			return -1;
		}
	}

	/**
	 * 更新回复的内容、时间
	 * 
	 * @param reply
	 * @return
	 */
	public int updateReply(Reply reply) {
		String sql = "update reply set content = ?,uploadTime = ? where id = ?";
		try {
			return replyDaoImpl.update(sql, reply.getContent(), reply.getUploadTime(), reply.getId());
		} catch (SQLException e) {
			e.printStackTrace();
			return -1;
		}
	}
}
